//
//  OrderMangerView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/21.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class OrderMangerView: UIView {
    
    // 滑动View
    private let scrollView = UIScrollView()
    // 没有更多数据
    private let nomoredata = NoMoreDataView()
    
    // MARK: 订单数据
    public var orders:[OrderInfo]? {
        didSet {
            updateLayout()
        }
    }
    
    // MARK: 回调
    public var complete:((_ item:OrderInfo) -> Void)?
    
    override func layoutSubviews() {
        guard let infos = orders else {
            return
        }
        if infos.count == 0 {
            return
        }
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        var orderHeight:CGFloat = 0
        for i in 0..<infos.count {
            let order = infos[i]
            // 背景颜色
            let backround = UIView()
            backround.tag = i
            // 图片
            let pic = UIImageView(image: UIImage(named: "ticket_30x30"))
            pic.frame.origin = CGPoint(x: MARGIN, y: MARGIN)
            backround.addSubview(pic)
            // 机票 文本
            let ticketText = UILabel(title: "机票")
            ticketText.textColor = COLOR_NICE
            ticketText.frame = CGRect(x: pic.frame.maxX + MARGIN10, y: MARGIN, width: MARGIN30, height: MARGIN)
            backround.addSubview(ticketText)
            // 创建时间
            let createTime = UILabel()
            createTime.text = order.createtime
            createTime.textColor = UIColor.gray
            createTime.font = UIFont.systemFont(ofSize: 11)
            let createTimeX = pic.frame.maxX + MARGIN10
            createTime.frame = CGRect(x: createTimeX, y: ticketText.frame.maxY + 2, width: MARGIN140, height: MARGIN)
            backround.addSubview(createTime)
            // 序号
            let orderStatus = UILabel()
            orderStatus.textColor = COLOR_NICE
            orderStatus.textAlignment = .right
            orderStatus.text = "第\(i + 1)条"
            orderStatus.font = UIFont.systemFont(ofSize: 14)
            orderStatus.frame = CGRect(x: frame.width - MARGIN100 - MARGIN, y: MARGIN25 , width: MARGIN100, height: MARGIN)
            backround.addSubview(orderStatus)
            // 下划线
            let line = UIView()
            line.backgroundColor = COLOR_LINE2
            let lineY = createTime.frame.maxY + MARGIN15
            line.frame = CGRect(x: MARGIN, y:  lineY, width: frame.width - MARGIN * 2, height: 1)
            backround.addSubview(line)
            // 出发时间
            let departTerminal = UILabel()
            departTerminal.text = order.departtime
            departTerminal.textColor = UIColor.black
            departTerminal.font = UIFont.systemFont(ofSize: 26)
            departTerminal.frame = CGRect(x: MARGIN, y: line.frame.maxY + MARGIN20 , width: MARGIN140, height: MARGIN20)
            backround.addSubview(departTerminal)
            // 到达时间
            let arrivalTerminal = UILabel()
            arrivalTerminal.text = order.arrivaltime
            arrivalTerminal.textAlignment = .right
            arrivalTerminal.textColor = UIColor.black
            arrivalTerminal.font = UIFont.systemFont(ofSize: 26)
            let arrivalX = frame.width - MARGIN140 - MARGIN
            arrivalTerminal.frame = CGRect(x: arrivalX, y: line.frame.maxY + MARGIN20 , width: MARGIN140, height: MARGIN20)
            backround.addSubview(arrivalTerminal)
            // 箭头图片
            let arrowPic = UIImageView(image: UIImage(named: "arrow_to_nav_32x7"))
            arrowPic.frame.origin = CGPoint(x: center.x - arrowPic.frame.width * 0.5, y: departTerminal.frame.maxY)
            backround.addSubview(arrowPic)
            // 出发航站楼
            let departTime = UILabel()
            departTime.text = order.city! + "机场" + order.departterminal!
            departTime.font = UIFont.systemFont(ofSize: 12)
            departTime.frame = CGRect(x: MARGIN, y: arrivalTerminal.frame.maxY + MARGIN , width: MARGIN140, height: MARGIN)
            backround.addSubview(departTime)
            // 到达航站楼
            let arrivalTime = UILabel()
            arrivalTime.textAlignment = .right
            arrivalTime.text = order.endcity! + "机场" + order.arrivalterminal!
            arrivalTime.font = UIFont.systemFont(ofSize: 12)
            arrivalTime.frame = CGRect(x: frame.width - MARGIN140 - MARGIN, y: arrivalTerminal.frame.maxY + MARGIN , width: MARGIN140, height: MARGIN)
            backround.addSubview(arrivalTime)
            // 下划线
            let line2 = UIView()
            if i != infos.count - 1 {
                line2.backgroundColor = COLOR_LINE
            }
            let line2Y = arrivalTime.frame.maxY + MARGIN20
            line2.frame = CGRect(x: MARGIN, y: line2Y, width: frame.width - MARGIN * 2, height: 1)
            backround.addSubview(line2)
            // 背景颜色
            backround.backgroundColor = UIColor.white
            backround.frame = CGRect(x: 0, y: orderHeight, width: frame.width, height: line2.frame.maxY)
            scrollView.addSubview(backround)
            // 每条高度
            orderHeight += line2.frame.maxY
            // 点击事件
            backround.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickItem(ui:))))
        }
        // 没有更多数据
        nomoredata.frame = CGRect(x: 0, y: orderHeight, width: frame.width, height: MARGIN55)
        scrollView.addSubview(nomoredata)
        // 设置scrollView属性
        scrollView.contentSize = CGSize(width: 0, height: nomoredata.frame.maxY + MARGIN20)
        addSubview(scrollView)
    }
    
    @objc func clickItem(ui:UITapGestureRecognizer) -> Void {
        if let callback = complete,let index = ui.view?.tag {
            callback(orders![index])
        }
    }
}
